
//get co2 intensity data
import delimited using ../Raw/ourworldindata/carbon-intensity-electricity.csv, clear
tempfile co2intensity
save `co2intensity'

//get country-production type outputs
import delimited using ../Raw/ourworldindata/electricity-prod-source-stacked.csv, clear
rename otherrenewablesexcludingbioenerg electricityfromotherrenewtwh
egen electricity_total_twh = rowtotal(electricity*)

//merge to co2 intensity
merge 1:1 entity year using `co2intensity', keep(3) nogen
drop if year < 2000 | year == 2022


foreach var of var electricityfrom* {
	replace `var' = 0 if missing(`var')
	gen s_`var' = `var'/electricity_total_twh
}
 



//clean up country names
kountry entity, from(other) geo(marc)
replace NAMES_STD = "Czech Republic" if NAMES_STD == "czechia"
replace GEO = "Africa" if inlist(NAMES_STD,"eswatini")
replace GEO = "Asia" if inlist(NAMES_STD,"Hong Kong","Taiwan","Myanmar")
drop code 
rename entity Country_co2
replace GEO = "Asia" if inlist(GEO,"West Indies", "Indian Ocean","Pacific Ocean")
replace GEO = "Europe" if inlist(NAMES_STD,"Czech Republic")
drop if missing(GEO) 
rename GEO region_co2
rename s_electricityfrom*twh share_*
rename electricityfrom*twh *_twh


rename carbonintensityofelectricitygco2 co2e_kwh 

rename NAMES_STD Country
order Country
order region_co2, after(Country)

compress

save ../Intermediate/country_power_co2intensity, replace




